"""
    @Author  ：思念 
    @File    ：8.bs4简单使用.py
    @Date    ：2024/12/6 10:37 
"""
from bs4 import BeautifulSoup

html = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" name="dromouse"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""

# 1.创建 Beautiful Soup 对象
soup = BeautifulSoup(html, "lxml")
# 格式化输出html代码
# print(soup.prettify())

# 2.find_all 查找所有对应标签，输出结果为列表
res = soup.find_all("a")
print(res)

# 3.find 获取出现第一次出现的a标签,查找对应标签的内容，输出结果为内容本身
res = soup.find("a")
print(res)

# 4.获取出现第一次出现的a标签
res = soup.find("a")
print(res)

# 5.通过属性查找指定标签
res = soup.find("a", attrs={"id": "link2"})
print(res)
res = soup.find("a", id='link2')
print(res)
res = soup.find_all("a", attrs={'class': 'sister'})  # 获取class属性为sister的所有a标签
print(res)
res = soup.find("p", class_='title')
print(res)
